package com.ruoyi.attachedRecord.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedData.service.IAttachedDataService;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.engineering.domain.Engineering;
import com.ruoyi.engineering.service.IEngineeringService;
import com.ruoyi.instrument.domain.Instrument;
import com.ruoyi.instrument.service.IInstrumentService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.workload.domain.Workload;
import com.ruoyi.workload.mapper.WorkloadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.attachedRecord.mapper.AttachedRecordMapper;
import com.ruoyi.attachedRecord.domain.AttachedRecord;
import com.ruoyi.attachedRecord.service.IAttachedRecordService;

import javax.annotation.Resource;

/**
 * 检测记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Service
public class AttachedRecordServiceImpl implements IAttachedRecordService {
    @Autowired
    private AttachedRecordMapper attachedRecordMapper;

    @Resource
    private IAttachedDataService attachedDataService;

    @Resource
    private IInstrumentService instrumentService;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private IEngineeringService engineeringService;

    @Autowired
    private ISysDictDataService dictDataService;

    @Resource
    private WorkloadMapper workloadMapper;

    /**
     * 查询检测记录
     *
     * @param id 检测记录主键
     * @return 检测记录
     */
    @Override
    public AttachedRecord selectAttachedRecordById(Long id) {
        return attachedRecordMapper.selectAttachedRecordById(id);
    }

    /**
     * 查询检测记录列表
     *
     * @param attachedRecord 检测记录
     * @return 检测记录
     */
    @Override
    public List<AttachedRecord> selectAttachedRecordList(AttachedRecord attachedRecord) {
        List<AttachedRecord> attList = attachedRecordMapper.selectAttachedRecordList(attachedRecord);
        if (!attList.isEmpty()) {
            for (AttachedRecord att : attList) {
                if (StringUtils.isNotEmpty(att.getTestInstrument())) {
                    String[] fruits = att.getTestInstrument().split(",");
                    List<String> fruitsList = new ArrayList<>();
                    for (String fruit : fruits) {
                        Instrument ii = instrumentService.selectInstrumentById(Long.parseLong(fruit));
                        if(ii != null && StringUtils.isNotEmpty(ii.getInstrumentName())){
                            fruitsList.add(ii.getInstrumentName());
                        }
                    }
                    if(StringUtils.isNotEmpty(fruitsList)){
                        att.setTestInstrument(String.join(",",fruitsList));
                    }
                }
            }
        }
        return attList;
    }

    @Override
    public Long selectAttachedRecordListForSum(AttachedRecord attachedRecord){

        return attachedRecordMapper.selectAttachedRecordListForSum(attachedRecord);
    }

    /**
     * 新增检测记录
     *
     * @param attachedRecord 检测记录
     * @return 结果
     */
    @Override
    public int insertAttachedRecord(AttachedRecord attachedRecord) {
        attachedRecord.setCreateTime(DateUtils.getNowDate());
        Integer isAcr = attachedRecordMapper.insertAttachedRecord(attachedRecord);
        //原始编号
        Long id = attachedRecord.getId();
        AttachedData attachedDataOne = attachedDataService.selectAttachedDataById(attachedRecord.getAttachedDataId());

        //生成报告编号  工程编号➕JL➕分类编码➕年份后两位➕序号
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR); // 获取当前年份

        Long commissionId = attachedRecord.getCommissionId();
        Commission commissionOne = commissionService.selectCommissionById(commissionId);

        Engineering engineeringOne = engineeringService.selectEngineeringById(commissionOne.getEngineeringId());

        //分类编码
        String fenleiNumber = "";
        List<SysDictData> sysDictDatalist = dictDataService.selectDictDataByDictType("detection_item_scene");
        for (SysDictData sysDictData : sysDictDatalist) {
            if (sysDictData.getDictLabel().equals(attachedDataOne.getTestMethod())) {
                fenleiNumber = sysDictData.getDictValue();
            }
        }

        String year2 = String.valueOf(year);
        String lastTwoChars = year2.substring(year2.length() - 2);
        String id_str = "";
        if(id < 10L){
            id_str = "00"+String.valueOf(id);
        }else if(id < 100L && id >= 10L){
            id_str = "0"+String.valueOf(id);
        }else {
            id_str = String.valueOf(id);
        }
        String number = engineeringOne.getEngineeringNumber()+"JL"+ fenleiNumber + lastTwoChars + id_str;

        AttachedRecord attachedRecord2 = new AttachedRecord();
        attachedRecord2.setId(id);
        attachedRecord2.setOriginalNumber(number);
        attachedRecordMapper.updateAttachedRecord(attachedRecord2);

        //新增工作量统计
        Workload workload = new Workload();
        BigDecimal testNumber = new BigDecimal(attachedRecord.getTestNumber());
        BigDecimal hours = new BigDecimal(1);
        //低应变
        if("360".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(30),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
               if(testNumber.compareTo(BigDecimal.valueOf(500)) > 0){
                   hours = testNumber.subtract(BigDecimal.valueOf(500)).divide(BigDecimal.valueOf(30),0,
                           RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale
                           (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(400).divide(BigDecimal.valueOf(30),0,
                           RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                           (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(100).divide(BigDecimal.valueOf(30),0,
                           RoundingMode.HALF_UP));
               } else if (testNumber.compareTo(BigDecimal.valueOf(100)) > 0) {
                   hours = testNumber.subtract(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(30),0,
                           RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                           (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(100).divide(BigDecimal.valueOf(30),0,
                           RoundingMode.HALF_UP));
               }else{
                   hours = testNumber.divide(BigDecimal.valueOf(30),0, RoundingMode.HALF_UP);
               }
            }
            //高应变、声波透射
        }else if("385".equals(attachedRecord.getTestProject()) || "386".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(4),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(60)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(60)).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(20).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(20).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(20).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(40)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(40)).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(20).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(20).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(20)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(20)).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(20).divide(BigDecimal.valueOf(4),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(4),0, RoundingMode.HALF_UP);
                }
            }
         //锚杆拉拔、单桩竖向抗压、单桩竖向抗拔、单桩水平抗压
        }else if("381".equals(attachedRecord.getTestProject()) || "382".equals(attachedRecord.getTestProject()) ||
                "383".equals(attachedRecord.getTestProject()) || "393".equals(attachedRecord.getTestProject()) ){
            BigDecimal decimal = new BigDecimal(3);
            if(testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(100)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(50).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(50)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(50)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(15)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP);
                }
            }
            //钻芯法
        }else if("384".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(100)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(50).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(50)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(50)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(15)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP);
                }
            }
        }else if("387".equals(attachedRecord.getTestProject()) || "388".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(50)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(50)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(15)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(100).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP);
                }
            }
            //应力
        }else if("413".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(50)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(50)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(15)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP);
                }
            }
            //动力触探
        }else if("414".equals(attachedRecord.getTestProject()) || "415".equals(attachedRecord.getTestProject()) ||"416".equals(attachedRecord.getTestProject())){
                if(testNumber.compareTo(BigDecimal.valueOf(50)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(50)).divide(BigDecimal.valueOf(5),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(35).divide(BigDecimal.valueOf(5),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(5),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(15)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(15)).divide(BigDecimal.valueOf(5),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(15).divide(BigDecimal.valueOf(5),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(5),0, RoundingMode.HALF_UP);
                }
                //基准网复核-水平
        }else if("417".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(100),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(2000)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(2000)).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(1500).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(500).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(500)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(500)).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(500).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(100),0, RoundingMode.HALF_UP);
                }
            }
            //基准网复核-竖向
        }else if("418".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(20)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(20)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(10).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(10).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(10)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(10)).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(10).divide(BigDecimal.valueOf(2),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(2),0, RoundingMode.HALF_UP);
                }
            }
            //水平位移、竖向位移监测（沉降观测）
        }else if("419".equals(attachedRecord.getTestProject())||"420".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(100),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(5000)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(5000)).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(4500).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(500).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(500)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(500)).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(500).divide(BigDecimal.valueOf(100),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(100),0, RoundingMode.HALF_UP);
                }
            }
            //地表裂缝监测
        }else if("421".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(200),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(2000)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(2000)).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(1000).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(1000).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(1000)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(1000)).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(1000).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(200),0, RoundingMode.HALF_UP);
                }
            }
            //锚杆拉力、结构应力监测
        }else if("422".equals(attachedRecord.getTestProject()) || "423".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(30),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(500)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(500)).divide(BigDecimal.valueOf(30),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(400).divide(BigDecimal.valueOf(30),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(100).divide(BigDecimal.valueOf(30),0,
                            RoundingMode.HALF_UP)).add(BigDecimal.valueOf(1));
                } else if (testNumber.compareTo(BigDecimal.valueOf(100)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(30),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.5")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(100).divide(BigDecimal.valueOf(30),0,
                            RoundingMode.HALF_UP)).add(BigDecimal.valueOf(1));;
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(30),0, RoundingMode.HALF_UP).add(BigDecimal.valueOf(1));;
                }
            }
            //地下水位监测、深层土体位移
        }else if("424".equals(attachedRecord.getTestProject()) || "425".equals(attachedRecord.getTestProject())){
            BigDecimal decimal = new BigDecimal(2);
            if(testNumber.divide(BigDecimal.valueOf(200),0, RoundingMode.HALF_UP).compareTo(decimal) <= 0){
                hours = decimal;
            }else{
                if(testNumber.compareTo(BigDecimal.valueOf(2000)) > 0){
                    hours = testNumber.subtract(BigDecimal.valueOf(2000)).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.4")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(1500).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP)).add(BigDecimal.valueOf(500).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP));
                } else if (testNumber.compareTo(BigDecimal.valueOf(500)) > 0) {
                    hours = testNumber.subtract(BigDecimal.valueOf(500)).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP).multiply(new BigDecimal("0.6")).setScale
                            (0,RoundingMode.HALF_UP).add(BigDecimal.valueOf(500).divide(BigDecimal.valueOf(200),0,
                            RoundingMode.HALF_UP));
                }else{
                    hours = testNumber.divide(BigDecimal.valueOf(200),0, RoundingMode.HALF_UP);
                }
            }
        }
        workload.setEngineeringId(attachedRecord.getEngineeringId());
        workload.setSampleId(attachedRecord.getAttachedDataId());
        workload.setChargeHours(String.valueOf(hours));
        workloadMapper.insertWorkload(workload);
        return isAcr;
    }

    /**
     * 修改检测记录
     *
     * @param attachedRecord 检测记录
     * @return 结果
     */
    @Override
    public int updateAttachedRecord(AttachedRecord attachedRecord) {
        attachedRecord.setUpdateTime(DateUtils.getNowDate());
        attachedRecordMapper.updateAttachedRecord(attachedRecord);

        AttachedRecord attachedRecordOne = attachedRecordMapper.selectAttachedRecordById(attachedRecord.getId());

        /*AttachedRecord attachedRecord2 = new AttachedRecord();
        attachedRecord2.setCommissionId(attachedRecordOne.getCommissionId());
        Long numberSum = attachedRecordMapper.selectAttachedRecordListForSum(attachedRecord2);

        System.out.println("numberSum");
        System.out.println(numberSum);*/
        /*AttachedData att = new AttachedData();
        att.setId(attachedRecord.getAttachedDataId());
        List<AttachedData> attList = attachedDataService.selectAttachedDataList(att);
        if (!attList.isEmpty()) {
            if (Integer.parseInt(attList.get(0).getCompletionProgress()) >= Integer.parseInt(attList.get(0).getAmount())) {
                att.setStatus(2L);
                attachedDataService.updateAttachedData(att);
            }
        }*/

        Integer completionNumber = 0;
        AttachedRecord attachedRecord2 = new AttachedRecord();
        attachedRecord2.setCommissionId(attachedRecordOne.getCommissionId());
        List<AttachedRecord> attachedRecordList = attachedRecordMapper.selectAttachedRecordList(attachedRecord2);
        if (!attachedRecordList.isEmpty()) {
            for (AttachedRecord attachedData2 : attachedRecordList) {
                if(StringUtils.isNotEmpty(attachedData2.getTestNumber())){
                    completionNumber += Integer.parseInt(attachedData2.getTestNumber());
                }
            }
        }

        AttachedData attOne = attachedDataService.selectAttachedDataById(attachedRecordOne.getAttachedDataId());
        AttachedData att = new AttachedData();
        if (completionNumber >= Integer.parseInt(attOne.getAmount())) {
            att.setStatus(2L);
            attachedDataService.updateAttachedData(att);
        }
        return 1;
    }

    /**
     * 批量删除检测记录
     *
     * @param ids 需要删除的检测记录主键
     * @return 结果
     */
    @Override
    public int deleteAttachedRecordByIds(Long[] ids) {
        return attachedRecordMapper.deleteAttachedRecordByIds(ids);
    }


    /**
     * 批量逻辑删除检测记录
     *
     * @param ids 需要删除的检测记录主键
     * @return 结果
     */
    @Override
    public int logicDeleteAttachedRecord(Long[] ids, String delType, Long delUser) {
        return attachedRecordMapper.logicDeleteAttachedRecord(ids, delType, delUser);
    }

    /**
     * 删除检测记录信息
     *
     * @param id 检测记录主键
     * @return 结果
     */
    @Override
    public int deleteAttachedRecordById(Long id) {
        return attachedRecordMapper.deleteAttachedRecordById(id);
    }
}
